//
//  ViewController.m
//  24-Homework
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+ZHWMD5.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *encryptTextField;
@property (weak, nonatomic) IBOutlet UITextField *decryptTextField;
@property (weak, nonatomic) IBOutlet UILabel *md5Label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)encryptAction:(id)sender {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *encryptStr = @"http://tool.zzblo.com/api/md5/encrypt";
    NSString *parameters = [NSString stringWithFormat:@"text=%@", _encryptTextField.text];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];

    [manager POST:encryptStr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", [responseObject class]);
        NSDictionary *dic = responseObject;
        _encryptTextField.text = nil;
        _decryptTextField.text = [dic objectForKey:@"secret16"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (IBAction)decryptAction:(id)sender {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *decryptStr = @"http://tool.zzblo.com/api/md5/decrypt";
    NSString *parameters = [NSString stringWithFormat:@"secret=%@", _decryptTextField.text];

    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    
    [manager POST:decryptStr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        _encryptTextField.text = [dic objectForKey:@"text"];
        _decryptTextField.text = nil;
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (IBAction)md5Action:(id)sender {
    _md5Label.text = [_decryptTextField.text stringWith16BitMD5Lower];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
